const Koa = require("koa")
const Router = require("koa-router")
const static = require("koa-static")
const parser = require("koa-bodyparser")
const { exex } = require("./db")

const app = new Koa()
const router = new Router()

app.use(parser())
app.use(static(__dirname + "/views"))

router.post("/getGoods",async ctx =>{
    const {userName,passWord } = ctx.request.body
    const data = await exex(`select * from 0923KS where userName='${userName}'`)
    if(data.length>0){
        ctx.body={
            message:"改用户已存在",
            code:1,
        }
    }
    else{
        await exex(`insert into  0923KS (userName,passWord) values ('${userName}','${passWord}')`)
        ctx.body={
            message:"注册成功",
            code:0,
        }
    }
})
router.post("/login",async ctx =>{
    const {userName,passWord } = ctx.request.body
    const data = await exex(`select * from 0923KS where userName='${userName}' and passWord='${passWord}'`)
    if(data.length>0){
        ctx.body={
             message:"登录成功",
             code:0,
        }
    }
    
    else{
        ctx.body={
            message:"未注册",
            code:1,
        }
    }
})

app.use(router.routes())
app.listen(4444,()=>{
    console.log("http://127.0.0.1:4444");
})